package com.zsinfo.guoss.supplier.activity;

import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.netcenter.Event.Response;
import com.example.netcenter.RetrofitBuilder;
import com.example.netcenter.Url.ConstantsCode;
import com.example.netcenter.bean.NewProductBean;
import com.example.netcenter.bean.ResultsData;
import com.zsinfo.guoss.supplier.R;
import com.zsinfo.guoss.supplier.base.BaseActivity;

public class NewProductDetailActivity extends BaseActivity {

    private TextView tv_time;
    private TextView tv_state;
    private ImageView iv1;
    private ImageView iv2;
    private ImageView iv3;
    private TextView tv_product_name;
    private TextView tv_product_place;
    private TextView tv_product_packaging;
    private TextView tv_new_produce_size;
    private TextView tv_new_produce_price;
    private TextView tv_remark;

    @Override
    protected int getContentLayoutRes() {
        return R.layout.activity_new_product_detail;
    }

    @Override
    protected void initView() {
        setTitle("上新详情");
        setBackAndLeftTitle("").setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        tv_time = findViewById(R.id.tv_time);
        tv_state = findViewById(R.id.tv_state);
        iv1 = findViewById(R.id.iv1);
        iv2 = findViewById(R.id.iv2);
        iv3 = findViewById(R.id.iv3);
        tv_product_name = findViewById(R.id.tv_product_name);
        tv_product_place = findViewById(R.id.tv_product_place);
        tv_product_packaging = findViewById(R.id.tv_product_packaging);
        tv_new_produce_size = findViewById(R.id.tv_new_produce_size);
        tv_new_produce_price = findViewById(R.id.tv_new_produce_price);
        tv_remark = findViewById(R.id.tv_remark);
    }

    @Override
    protected void initNetData() {
        String newGoodsId = getIntent().getStringExtra("newGoodsId");
        GetNetCenter().GetNetCenter(this,
                RetrofitBuilder.CreateApi().NEW_GOOD_DETAILS(ConstantsCode.new_goods_details, newGoodsId), new Response() {
                    @Override
                    public void OK(Object data) {
                        Log.e("lixl", data.toString());
                        setView(data);
                    }

                    @Override
                    public void Fail(Throwable e) {
                        Log.e("lixl", e.getMessage());
                    }

                    @Override
                    public void complete() {

                    }
                });
    }

    @Override
    protected void customDestroy() {

    }

    private void setView(Object data){
        ResultsData<NewProductBean> resultsData = (ResultsData<NewProductBean>) data;
        NewProductBean newProductBean = resultsData.getData();
        tv_time.setText(newProductBean.getCreateTime());
        //判断审核状态：0审核中，1审核成功，-1审核失败
        int status = newProductBean.getStatus();
        if (status == 1){
            tv_state.setText("审核成功");
            tv_state.setTextColor(getResources().getColor( R.color.green));
        } else if (status == 0){
            tv_state.setText("审核中");
            tv_state.setTextColor(getResources().getColor( R.color.red));
        } else if (status == -1){
            tv_state.setText("审核失败");
            tv_state.setTextColor(getResources().getColor(R.color.gray));
        }
        tv_product_name.setText(newProductBean.getGoodsName());
        tv_product_place.setText(newProductBean.getSourceCity());
        tv_product_packaging.setText(newProductBean.getPacking());
        tv_new_produce_size.setText(newProductBean.getSizeDesc());
        tv_new_produce_price.setText(newProductBean.getPrice()+"元/"+ newProductBean.getPriceUnit());
        tv_remark.setText(newProductBean.getNote());
       // 判断是否显示 图片
        String imgs = newProductBean.getImgs();
        showPicture(imgs.split("@"));
    }

    /**
     * 图片显示处理
     * @param imgs  分隔@字符串
     */
    private void showPicture(String[] imgs){
        //获取图片数量
        int count = imgs.length;
        switch (count){
            case 1:
                //一张图片
                iv1.setVisibility(View.VISIBLE);
                iv2.setVisibility(View.GONE);
                iv3.setVisibility(View.GONE);
                Glide.with(this).load(imgs[0]).into(iv1);
                break;
            case 2:
                //两张图片
                iv1.setVisibility(View.VISIBLE);
                iv2.setVisibility(View.VISIBLE);
                iv3.setVisibility(View.GONE);
                Glide.with(this).load(imgs[0]).into(iv1);
                Glide.with(this).load(imgs[1]).into(iv2);
                break;
            case 3:
                //三张图片
                iv1.setVisibility(View.VISIBLE);
                iv2.setVisibility(View.VISIBLE);
                iv3.setVisibility(View.VISIBLE);
                Glide.with(this).load(imgs[0]).into(iv1);
                Glide.with(this).load(imgs[1]).into(iv2);
                Glide.with(this).load(imgs[2]).into(iv3);
                break;
            default:
                //都不显示
                iv1.setVisibility(View.GONE);
                iv2.setVisibility(View.GONE);
                iv3.setVisibility(View.GONE);
                break;
        }
    }
}
